package com.carcenter.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.carcenter.store.entity.StoreChina;
import com.carcenter.store.mapper.StoreChinaMapper;
import com.carcenter.store.service.IStoreChinaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author car
 * @since 2025-07-16
 */
@Service
public class StoreChinaServiceImpl extends ServiceImpl<StoreChinaMapper, StoreChina> implements IStoreChinaService {

    @Autowired
    private StoreChinaMapper storeChinaMapper;
    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public List<StoreChina> getAddress(Integer pid, String key) {
        Boolean flag=redisTemplate.hasKey(key);
        if(flag){
            Set<StoreChina> set = redisTemplate.opsForZSet().range(key, 0, -1);
            List<StoreChina> list=new ArrayList<>();
            set.forEach(storeChina -> {list.add(storeChina);});
            return list;
        }else {
            LambdaQueryWrapper<StoreChina> wrapper = Wrappers.lambdaQuery(StoreChina.class)
                    .eq(StoreChina::getPid, pid)
                    .ne(StoreChina::getId, 0)
                    .orderByAsc(StoreChina::getId);
            List<StoreChina> list = storeChinaMapper.selectList(wrapper);
            list.forEach(storeChina -> {
                redisTemplate.opsForZSet().add(key, storeChina, storeChina.getId());
            });
            return list;
        }



    }
}
